import {Module} from "vuex";
import {RouteLocationNormalizedLoaded} from "vue-router";
const ADD_VISITED_VIEWS = 'ADD_VISITED_VIEWS'
const DEL_VISITED_VIEWS = 'DEL_VISITED_VIEWS'
const DEL_ALL_VISITED_VIEWS = 'DEL_ALL_VISITED_VIEWS'
const DEL_OTHER_VISITED_VIEWS = 'DEL_OTHER_VISITED_VIEWS'
const ADD_CACHE_VIEWS = 'ADD_CACHE_VIEWS'
const DEL_CACHE_VIEWS = 'DEL_CACHE_VIEWS'
function getTagName(tag: RouteLocationNormalizedLoaded) {
    if(tag.matched){
        const findTag = tag.matched.find(t=>t.path === tag.path)
        if(findTag){
            //获取路由组件的name,keep-alive 缓存 要和组件name 值一致
            return (findTag.components.default.name as string)
        }
    }else {
        return (tag.name as string)
    }
}
function delCacheView(visitedViews: RouteLocationNormalizedLoaded[],cacheViews: string[]) {
    const tagNames =  visitedViews.map(getTagName)
    return  cacheViews.filter(c=>tagNames.includes(c))
}
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
interface ITagView {
    // eslint-disable-next-line
    visitedViews: RouteLocationNormalizedLoaded[];
    cacheViews: string[];
}
// eslint-disable-next-line
const tagViews: Module<ITagView, any> ={
    state:{
        visitedViews: [],
        cacheViews:[]
    },
    mutations:{
        [ADD_VISITED_VIEWS](state,tag: RouteLocationNormalizedLoaded){
            if(tag.path.startsWith('/redirect') || state.visitedViews.some((t)=>t.path === tag.path)){
                return
            }
            state.visitedViews.push( Object.assign({},tag,{meta: { ...tag.meta, title: tag.meta.title || 'no-name'}}))
        },
        [DEL_VISITED_VIEWS](state,tag: RouteLocationNormalizedLoaded){
            const index = state.visitedViews.findIndex(t=>t.path === tag.path)
            if(index>-1){
                state.visitedViews.splice(index,1)
                const tagName =getTagName(tag)
                state.cacheViews = state.cacheViews.filter(name=> name !== tagName)
            }
        },
        [DEL_ALL_VISITED_VIEWS](state){
            state.visitedViews = state.visitedViews.filter(t=>t.meta&&t.meta.isAffix)
            state.cacheViews = delCacheView(state.visitedViews,state.cacheViews)
        },
        [DEL_OTHER_VISITED_VIEWS](state, tag: RouteLocationNormalizedLoaded){
            state.visitedViews = state.visitedViews.filter(t=>t.meta&&t.meta.isAffix || t.path === tag.path)
            state.cacheViews = delCacheView(state.visitedViews,state.cacheViews)
        },
        [ADD_CACHE_VIEWS](state,tag: RouteLocationNormalizedLoaded){
            const name = getTagName(tag)
            if(name && !state.cacheViews.includes(name)){
                state.cacheViews.push(name)
            }
        },
        [DEL_CACHE_VIEWS](state,tag: RouteLocationNormalizedLoaded){
            const tagName = getTagName(tag)
            state.cacheViews = state.cacheViews.filter(name=> name !== tagName)
        }
    },
    actions:{
        addVisitedViews({commit},tag: RouteLocationNormalizedLoaded){
            commit(ADD_VISITED_VIEWS,tag)
        },
        async delVisitedViews({commit},tag: RouteLocationNormalizedLoaded){
            await commit(DEL_VISITED_VIEWS,tag)
        },
        addTag({dispatch},tag: RouteLocationNormalizedLoaded){
            dispatch('addVisitedViews',tag)
            dispatch('addCacheViews',tag)
        },
        async delTag({dispatch},tag: RouteLocationNormalizedLoaded){
            await  dispatch('delVisitedViews',tag)
            await dispatch('delCacheView',tag)
        },
         delAllTag({commit}){
            commit(DEL_ALL_VISITED_VIEWS)
        },
        delOtherTag({commit},tag: RouteLocationNormalizedLoaded){
            commit(DEL_OTHER_VISITED_VIEWS,tag)
        },
        addCacheViews({commit},tag: RouteLocationNormalizedLoaded){
            commit(ADD_CACHE_VIEWS,tag)
        },
        async delCacheView({commit},tag: RouteLocationNormalizedLoaded){
           await  commit(DEL_CACHE_VIEWS,tag)
        }
    }
}
export default tagViews
